﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using BUS;
using DTO;
using DAO;

namespace BUS
{
    public class SanPhamBUS
    {
        SanPhamDAO spDao = new SanPhamDAO();

        public static DataTable Ds_SpIndex()
        {
            return SanPhamDAO.Ds_SpIndex();
        }

        //Lay sanh sach san pham moi nhat
        public static DataTable Ds_SpMoiNhat()
        {
            return SanPhamDAO.Ds_SpMoiNhat();
        }
        //Lay danh sach san pham theo danh muc
        public static DataTable Ds_SanPhamTheoDM(int id)
        {
            return SanPhamDAO.DsSanPhamTheoDM(id);
        }
        //Lay san pham theo thuong hieu
        public static DataTable Ds_SanPhamTheoTH(int id)
        {
            return SanPhamDAO.SanPhamThuongHieu(id);
        }
        //Chi Tiet San Pham
        public static SanPhamDTO ChiTietSanPham(int id)
        {
            SanPhamDTO sp = new SanPhamDTO();
            DataTable dt = SanPhamDAO.ChiTietSanPham(id);
            sp.MaSP = Int32.Parse(dt.Rows[0]["MaSP"].ToString());
            sp.TenSP = dt.Rows[0]["TenSP"].ToString();
            sp.Giagoc = Int32.Parse(dt.Rows[0]["GiaGoc"].ToString());
            if (dt.Rows[0]["GiaKM"].ToString() != "")
            {
                sp.GiaKM = Int32.Parse(dt.Rows[0]["GiaKM"].ToString());
            }else{
                sp.GiaKM = 0;
            }
            sp.Mota = dt.Rows[0]["MoTa"].ToString();
            sp.Chatlieu = dt.Rows[0]["ChatLieu"].ToString();
            sp.Trongluong = float.Parse(dt.Rows[0]["TrongLuong"].ToString());
            sp.Loaida = dt.Rows[0]["LoaiDa"].ToString();
            sp.SlTon = Int32.Parse(dt.Rows[0]["SLTon"].ToString());
            sp.MaDM = Int32.Parse(dt.Rows[0]["MaDM"].ToString());
            sp.MaTH = Int32.Parse(dt.Rows[0]["MaTH"].ToString());
            sp.HinhAnh = dt.Rows[0]["HinhAnh"].ToString();
            sp.HinhAnh2 = dt.Rows[0]["HinhAnh2"].ToString();
            sp.HinhAnh3 =  dt.Rows[0]["HinhAnh3"].ToString();
            sp.Viewed = Int32.Parse(dt.Rows[0]["Viewed"].ToString());
            sp.IsIndex = Int32.Parse(dt.Rows[0]["IsIndex"].ToString());
            return sp;
        }
        //San pham cung loai
        public static DataTable SanPhamCungLoai(int maDM)
        {
            return SanPhamDAO.SanPhamCungLoai(maDM);
        }
        //Update luot view san pham
        public static void UpdateView(int maSP)
        {
            int a = SanPhamDAO.UpdateView(maSP);
        }
        //Danh sach san pham xem nhieu
        public static DataTable SanPhamXemNhieu()
        {
            return SanPhamDAO.Ds_SPXemNhieu();
        }


        //Danh sach hinh anh cua sp
        public static DataTable DsHinhAnh(int id) {
            DataTable ds = new DataTable();
            DataTable dt = SanPhamDAO.ChiTietSanPham(id);
            ds.Rows.Add(new Object[] {"HinhAnh",dt.Rows[0]["HinhAnh"].ToString()});
            return ds;
        }


        //Lay danh sach san pham tu csdl
        public DataTable LoadDanhSachSanPham()
        {
            return spDao.LoadDanhSachSanPham();
        }
       
        //Insert san pham vao csdl
        public int ThemSanPham(SanPhamDTO spDTO)
        {
            return spDao.ThemSanPham(spDTO);
        }

        //Load danh muc
        public DataTable LoadDM()
        {
            return spDao.LoadDM();
        }

        //Load thuong hieu
        public static DataTable LoadTH()
        {
            return SanPhamDAO.LoadTH();
        }

        //Danh sach tat ca san pham
        public static DataTable DsSanPham()
        {
            return SanPhamDAO.DsSanPham();
        }

        //Cap nhat thong tin san pham
        public int CapNhatSanPham(SanPhamDTO spDTO)
        {
            return spDao.CapNhatSanPham(spDTO);
        }

        //Lay thong tin san pham theo masp
        public DataTable LoadDanhSachSanPhamTheoMa(int masp)
        {
            return spDao.LoadDanhSachSanPhamTheoMa(masp);
        }

	//tim kiem sap pham theo ten
        public static List<SanPhamDTO> TimKiemSanPham(String tensp )
        {
            return SanPhamDAO.TimKiemSanPham(tensp);
        }
    }
}
